package com.scrawler.util;



import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.springframework.stereotype.Service;




/**  
 * @Description: 描述
 * @author kuntang    
 * @version 1.0  
 * @created 2012-10-23 下午2:13:41 
 * Copyright (c) 2012 sohu,Inc. All Rights Reserved.
 */
@Service
public class HtmlParseUtils {

	
	public Set<String> getLinkStringByNodeClass(NodeList iterator,Class a,Log LOG){
		Set<String> urls = new LinkedHashSet<String>();
		int size = 0;
		if(iterator != null){
			size = iterator.size();
		}
		if(iterator != null && size>0 ){
			for(int i=0;i<size;i++){
				Node node = iterator.elementAt(i);
				if(node instanceof LinkTag){
					if(node.getClass() == a){
						String url = ((LinkTag)node).extractLink().trim(); 
						String title = ((LinkTag)node).toPlainTextString().trim();
						/**小于4个汉字的链接,过滤掉*/
						if(title == null || title.length()<=ConstantUtils.CHINESE_NUMBER_FILTER){
							LOG.info("过滤掉文章:"+url+","+title);
							continue;
						}
						/** 有可能是css或js标签,不符合url形式*/
						if(url != null && url.matches(ConstantUtils.URL_REGEXP_RULES)){
							urls.add(url);
						}else{				
							LOG.info(i+","+"过滤掉url: "+url);
						}
					}
				}
			}
		}
		return urls;
	}
	
	/**
	 * 解析百度热词榜的html,来获取关键词,点击量等信息.
	 * @Description:
	 * @Date: 2012-10-24
	 * @param:  nodes
	 * @return: Map<String,Integer> 返回<关键词-点击量>
	 */
	public Map<String,Integer> getKeyWordAndClickNum(NodeList nodes,int[] tdLocation){
		Map<String,Integer> keyWordClick = new LinkedHashMap<String,Integer>();
		if(nodes == null || nodes.size()<1){
			return keyWordClick;
		}
		System.out.println("getKeyWordAndClickNum.......");
		int keyLocation = tdLocation[0]-1;
		int clickNumLocation = tdLocation[1]-1;
		for(int i=1;i<nodes.size();i++){
			Node keyWordsNode = nodes.elementAt(i);
			String keyWord = keyWordsNode.toPlainTextString();
			Node clickNode = nodes.elementAt(++i);
			String clickNum = clickNode.toPlainTextString();
			//System.out.println(keyWord+","+clickNum);
			if(keyWord!=null&&!keyWord.isEmpty()&&clickNum != null && clickNum.matches("\\d+")){
				keyWordClick.put(keyWord, Integer.parseInt(clickNum));
			}		
		}
		return keyWordClick;
	}
	
	
	public String getNode2Html(NodeList nodes,Class tagClass){
		StringBuilder sb = new StringBuilder();
		int size = 0;
		if(nodes != null){
			size = nodes.size();
		}
		if(size>0 ){
			for(int i=0;i<size;i++){
				Node node = nodes.elementAt(i);
				if(node.getClass() == tagClass){
					sb.append(node.toHtml());
				}
			}
		}
		return sb.toString();
	}
	
	/**
	 * @Description:
	 * @Date: 2012-10-23
	 * @param: @param args
	 * @return: void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
